package snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener , ActionListener {
    //定义🐍蛇的数据结构
    int length;
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String fx;
    boolean isStart = false;
    boolean isFail = false;

    //食物的坐标
    int foodx;
    int foody;
    Random random =new Random();

    //得分
    int score;
    //定时器 1000ms执行了10次
    Timer timer = new Timer(100,this);  //已毫秒为单位

    //构造器
    public GamePanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start(); //游戏一开始定时器启动
    }

    //初始化方法
    public void init(){
        length = 3;
        fx = "R";
        snakeX[0] = 100;snakeY[0] = 100;    //脑袋
        snakeX[1] = 75;snakeY[1] = 100;    //第一节身体
        snakeX[2] = 50;snakeY[2] = 100;    //第二节身体
        //把食物随机分配到界面上
        foodx = 25 + 25*random.nextInt(34);
        foody = 75 + 25*random.nextInt(24);

        score = 0;

    }

    //绘制面板,我们游戏中的所有东西都用这只画笔
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.white);
        Data.header.paintIcon(this,g,25,11);        //头部广告栏
        g.fillRect(25,75,850,600);      //游戏界面

        //把小蛇画上去
        if(fx == "R")
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        else if(fx == "L")
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        else if(fx == "U")
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        else if(fx == "D")
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);

        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }
        if(!isStart){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按 下 空 格 开 始 游 戏",250,300);
        }
        if(isFail){
            g.setColor(Color.RED);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("失 败!",250,300);
            g.drawString("按 下 空 格 开 始 游 戏",250,400);
        }
        //画积分
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度: "+length,750,35);
        g.drawString("分数: "+score,750,55);

        Data.food.paintIcon(this,g,foodx,foody);

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_SPACE) {
            if(isFail){
                //重新开始
                isFail = false;
                init();
            }else{
                isStart = !isStart;
            }
            repaint();
        }
        if(keyCode == KeyEvent.VK_UP && fx != "D") {
            fx = "U";
        }
        if(keyCode == KeyEvent.VK_DOWN&& fx != "U") {
            fx = "D";
        }
        if(keyCode == KeyEvent.VK_LEFT&& fx != "R") {
            fx = "L";
        }
        if(keyCode == KeyEvent.VK_RIGHT&& fx != "L") {
            fx = "R";
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(isStart && !isFail){        //如果游戏是开始状态 小蛇就动
            if(snakeX[0] == foodx && snakeY[0] == foody){
                length++;
                score+=10;
                foodx = 25 + 25*random.nextInt(34);
                foody = 75 + 25*random.nextInt(24);
            }
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];            //后一节移动到前一节
            }
            if(fx.equals("R")) {
                snakeX[0] += 25;
            }
            else if(fx.equals("L")) {
                snakeX[0] -= 25;
            }
            else if(fx.equals("U")) {
                snakeY[0] -= 25;
            }
            else if(fx.equals("D")) {
                snakeY[0] += 25;
            }
            if(snakeX[0]> 850){
                snakeX[0] = 25;
            }
            else if(snakeX[0] < 25){
                snakeX[0] = 850;
            }
            if(snakeY[0] > 650){
                snakeY[0] = 75;
            }
            else if(snakeY[0] < 75){
                snakeY[0] = 650;
            }
            for (int i = 1; i < length; i++) {
                if(snakeY[0] == snakeY[i]&&snakeX[0] == snakeX[i]){
                    isFail = true;
                }
            }
            repaint();
        }
        timer.start();
    }
}
